package com.ruoyi.aitmsdz.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ServiceLog;
import com.ruoyi.aitcommon.service.SQLService;
import com.ruoyi.aitcommon.utils.IDGenUtils;

@Service
public class PLMService {
	@Autowired
	SQLService sqlservice;
	
	@ServiceLog("PLM项目保存")
	public void projectSave(List<Map<String,Object>> datas,JSONObject db) throws Exception {

		List<Map<String,Object>> insertList=new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> updateList=new ArrayList<Map<String,Object>>();
		for(Map<String,Object> data:datas) {
			
//			研制立项 才推送PLM
			if(data.get("field0055")==null||!data.get("field0055").toString().equals("研制立项"))
				continue;
			
//			构造数据
			String pk=IDGenUtils.getGuid();
			Map<String,Object> plmdata=new LinkedHashMap<String,Object>();
			plmdata.put("PROJECTID",pk);
			plmdata.put("PROJECTNAME",data.get("field0006"));
			plmdata.put("PROJECTTYPE",3);
			plmdata.put("PRINCIPAL",130);
			plmdata.put("DESIGNER","12"); //设计人员
			plmdata.put("UESER","");
			plmdata.put("CONTRACTID","ROOT");
			plmdata.put("BEGINTIME",data.get("field0041"));
			plmdata.put("ENDTIME",data.get("field0042"));
			plmdata.put("DETAIL",data.get("field0049"));
			plmdata.put("CFLOWID",3);
			plmdata.put("CFLOWVER",25);
			plmdata.put("CFLOWMODELID","");
			plmdata.put("CFLOWMODELVER","");
			plmdata.put("CSTATE","");
			plmdata.put("CPRIORITY","");
			plmdata.put("CREALBEGINTIME","");
			plmdata.put("CREALENDTIME","");
			plmdata.put("CMKINDID",85);
			plmdata.put("DOCKINDID",90);
			plmdata.put("PROJECTCODE",data.get("field0038"));
			plmdata.put("modifytime","");
			plmdata.put("warntime",80);
			plmdata.put("PROJECTDOCCONTROL","false");
			
//			查询项目
			String sql="select * from prj_017 where PROJECTCODE='%s'";
			sql=String.format(sql, data.get("field0038"));
			List<Map<String, Object>> oldlist = sqlservice.execQuery(sql, db);
			if(oldlist.size()>0) {
//				修改项目
				updateList.add(plmdata);
			}else {
//				新增项目
				insertList.add(plmdata);
			}
		}
		
//		更新数据库
		if(insertList.size()>0)
			sqlservice.mapInsert(insertList, db, "prj_017", null, null);
		if(updateList.size()>0) {
			String includes="PROJECTNAME,DETAIL,DESIGNER";
			String key="PROJECTCODE";
			sqlservice.mapUpdate(updateList, db, "prj_017", key, includes,null);
		}
		
	}
}
